package hx.controller;

import hx.pojo.Claim_voucher;
import hx.pojo.Employee;
import hx.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/EmployeeController")
public class EmployeeController {

    @Autowired
    private EmployeeService service;

    /**
     * 用于员工登录
     * @param user 员工表
     */
    @RequestMapping("/login")
    public String setService(Employee user, HttpServletRequest request, HttpServletResponse response) throws IOException {
        Employee login = service.Login_name(user.getNAME());         //根据输入的账号，查询出账号的所有数据

        response.setContentType("text/html;charset=UTF-8");     //为什么要处理中文，因为不处理登录错误返回信息都是乱码
        if (login!=null){                                       //查询的数据不为空
            if (login.getPASSWORD().equals(user.getPASSWORD())){//查询的数据库密码 等于 输入的密码
                HttpSession session = request.getSession();
                session.setAttribute("employee",login);
                session.setAttribute("position",login.getPosition());
                session.setAttribute("department",login.getDepartment());
                return "welcome";
            }else {
                response.getWriter().print("<script>alert('登录失败,密码错误！请刷新重试');window.location.href='../index.jsp'</script>");
            }
        }else{                                                  //查出的数据为空，表示没有账号有误
            response.getWriter().print("<script>alert('登录失败,账号错误！请刷新重试');window.location.href='../index.jsp'</script>");
        }
        return null;
    }

    /**
     * 用于添加报销凭证
     * @return
     */
    @RequestMapping("/addClaimVoucher")
    public String AddClaimVoucher(){

        return "Add_claim_voucher";
    }

    /**
     * 用于查看报销凭证
     */
    @RequestMapping("/selectClaimVoucher")
    public String selectClaimVoucher(HttpServletRequest request){
        HttpSession session = request.getSession();         //获取session
        Employee employee = (Employee) session.getAttribute("employee");
        System.out.println(employee.getSN());

        //把获取的session的 id值 写进 查询语句 里面
        List<Claim_voucher> claim_vouchers = service.voucher_selectSN("003");
        for (Claim_voucher a:claim_vouchers){
            System.out.println(a.getCreateDate());
        }
        request.setAttribute("claimVoucher",claim_vouchers);
        return "select_Claim_Voucher";
    }
}
